package mysqlConnecionMappersPackage;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dataConnectionPackage.DataConnector;
import dataConnectionPackage.IMapper;
import dataTypesPackage.Ulke;

public class UlkeMapper implements IMapper {
	private ResultSet rs;
	@Override
	public Object get(int ID) {
		// Gets ulke from database with given id
		rs = DataConnector.getInstance().query("select * from `ulke` where `ID` = " + ID + "");
		if (rs != null){
			try {
				rs.next();
				if (Integer.parseInt(rs.getString(1)) == 999 ){
					//ulke has:ID,name,gmt
					 Ulke ulke = new Ulke(Integer.parseInt(rs.getString(1)),rs.getString(2), Integer.parseInt(rs.getString(3)));
					 return ulke;
				}
				else
					System.out.println("you don't have acces to ulke with given ID");
			} catch (NumberFormatException e) {
				System.out.println("UlkeMapper: number format problem");
				e.printStackTrace();
			} catch (SQLException e) {
				System.out.println("UlkeMapper: sql problem");
				e.printStackTrace();
			}
		}
		else
			System.out.println("no ulke with given ID");
		return null;
	}

	@Override
	public List<Object> getAll() {
		// returns a list<Object> filled with Liman objects from DB
		
		List<Object> allLimanList = new ArrayList<Object>();
		rs = DataConnector.getInstance().query("select * from `ulke`;");
		//DataConnector.getInstance().printLastQueryResult();
        try {
        	if (rs != null){
        		while(rs.next()){
        			int numberOfRows = DataConnector.getInstance().countRows("ulke");
        			Ulke temp;
        			for (int i=1;i<=(numberOfRows*3);i+=3){			//table has 3 coloums. since we add 3 every loop, 3*row needed
        				//					ID,								name,	gmt,		
        				temp = new Ulke(Integer.parseInt(rs.getString(i)), rs.getString(i+1), Integer.parseInt(rs.getString(i+2)));
        				allLimanList.add(temp);
        			}               
        		}
        	}
        }
        catch(Exception e){
        	e.printStackTrace();
        }
		return allLimanList;
	}

	@Override
	public int set(Object object) {
		// TODO Auto-generated method stub
		return 0;
	}

}
